/*
@title: Photosynthesis
@description: "Photosynthesis" is a strategy game where you collect sunlight, water, and carbon dioxide to help a plant grow. As you gather these resources, the plant progresses to the next level, challenging you to collect the elements in more difficult levels.
@author: Vanessa Tan
@tags: []
@addedOn: 2024-08-26
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/

const player = "p";
const sunlight = "s";
const water = "w";
const co2 = "c";
const ground = "g";

setLegend(
  [ player, bitmap`
................
................
................
................
................
................
.....44.44......
.....44444......
......444.......
.......4........
.......4........
.......4........
....C..4..C.....
....CCCCCCC.....
.....CCCCC......
.....CCCCC......` ],
  [ sunlight, bitmap`
................
................
................
............6...
...........66...
.......6..66..6.
......66.66..66.
.....66..6..66..
....66..66.66...
....6..66..6....
...66..6..66....
..66..66.66.....
.....66..6......
...666..66......
.......66.......
................`
  ],
  [water, bitmap`
................
................
................
.......7........
......777.......
......777.......
.....77777......
.....77777......
....7777777.....
....7777777.....
....7777777.....
.....77777......
......777.......
................
................
................`],
  [co2, bitmap`
................
.....1...11.....
....11..11......
....1...1.......
....1...11......
....11...1......
.....1...11.....
.....11...1.....
......1...11....
.....11...11....
....11....1.....
....1.....1.....
....1.....1.....
....11...1......
.....11..1......
................`],
  [ground, bitmap`
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC`]
)

setSolids([])
setPushables({
  [ player ]: []
})

let level = 0
const levels = [
  map`
...s...
.......
...c...
.......
...w...
...p...
ggggggg`,
  map`
.......
....s..
.......
..c....
.......
...w...
...p...
ggggggg`, map`
......c
.......
.......
s......
.......
...w...
.......
...p...
ggggggg`,
  map`
.......
.s.....
.......
.......
.......
.......
...c...
w......
...p...
  ggggggg`,map`
......c
.......
.......
.......
s......
.......
.......
.......
.w.....
...p...
ggggggg`,map`
.......
w......
.......
......s
.......
.......
....c..
.......
.......
.......
...p...
ggggggg`,map`
......c
.......
.......
.......
.......
.......
.......
.......
.......
...s...
w......
...p...
ggggggg`,map`
......w
c......
.......
.......
.......
.......
.......
.......
.......
.......
.......
s......
...p...
ggggggg`
  
]
const currentLevel = levels[level];
setMap(currentLevel);

setSolids([])
let scoreSun = 0
let scoreWater = 0
let scoreCo2 = 0




setPushables({
  [ground]: []
})

var gameRunning = true; 

onInput("a", () => {
  if (gameRunning) {
    getFirst(player).x -= 1;
  }
});

onInput("d", () => {
  if (gameRunning) {
    getFirst(player).x += 1;
  }
});

onInput("j", () => {
  const currentLevel = levels[level]; // get the original map of the level

  // make sure the level exists before we load it
  if (currentLevel !== undefined) {
    clearText("");
    setMap(currentLevel);
    scoreSun = 0
    scoreWater = 0
    scoreCo2 = 0
  }
});

//moving stuff down
function moveSun() {
  let sunlights = getAll(sunlight);

  for (let i = 0; i < sunlights.length; i++) {
    sunlights[i].y += 1;
  }
  
}

function moveWater() {
  let waters = getAll(water);

  for (let i = 0; i < waters.length; i++) {
    waters[i].y += 1;
  } 
}
function moveCo2() {
  let co2s = getAll(co2);

  for (let i = 0; i < co2s.length; i++) {
    co2s[i].y += 1;
  }
  
}

//despawning stuff
function despawnSun() {
  let sunlights = getAll(sunlight);

  for (let i = 0; i < sunlights.length; i++) {
   if (sunlights[i].y == level+6) {
     sunlights[i].remove();
   }
  }
}

function despawnWater() {
  let waters = getAll(water);

  for (let i = 0; i < waters.length; i++) {
   if (waters[i].y == level+6) {
     waters[i].remove();
   }
  }
}

function despawnCo2() {
  let co2s = getAll(co2);

  for (let i = 0; i < co2s.length; i++) {
   if (co2s[i].y == level+6) {
     co2s[i].remove();
   }
  }
}


//check overlapping stuff
function checkSun() {
  let sunlights = getAll(sunlight);
  let p = getFirst(player);

  for (let i = 0; i < sunlights.length; i++) {
    if (sunlights[i].x == p.x && sunlights[i].y == p.y) {
      return true;
    }
  }

  return false;
}

function checkWater() {
  let waters = getAll(water);
  let p = getFirst(player);

  for (let i = 0; i < waters.length; i++) {
    if (waters[i].x == p.x && waters[i].y == p.y) {
      return true;
    }
  }

  return false;
}

function checkCo2() {
  let co2s = getAll(co2);
  let p = getFirst(player);

  for (let i = 0; i < co2s.length; i++) {
    if (co2s[i].x == p.x && co2s[i].y == p.y) {
      return true;
    }
  }

  return false;
}


var gameLoop = setInterval(() => {
  addText(`Level: ${level+1}`, {x: 2, y: 1, color: color`L`})
  moveSun();
  moveWater();
  moveCo2();
  
  despawnSun();
  despawnWater();
  despawnCo2();
  
  checkSun();
  checkWater();
  checkCo2();
  
  addText(`${scoreSun}`, { x: 4,y: 14, color: color`6` });
  addText(`${scoreWater}`, { x:5, y: 14, color: color`7` });
  addText(`${scoreCo2}`, { x:6, y: 14, color: color`1` });
  if (checkSun()) {
    scoreSun += 1;
    addText(`${scoreSun}`, { x: 4,y: 14, color: color`6` });
  }
    if (checkWater()) {
    scoreWater += 1;
       addText(`${scoreWater}`, { x:5, y: 14, color: color`7` });
  }
    if (checkCo2()) {
    scoreCo2 += 1;
       addText(`${scoreCo2}`, { x:6, y: 14, color: color`1` });
  }
  
  if (scoreSun == 1 && scoreWater == 1 && scoreCo2 == 1) {
    level += 1;
    
    
    if (levels[level] !== undefined) {
      setMap(levels[level]);
      scoreSun = 0;
    scoreWater = 0;
    scoreCo2 = 0;
    } else {
      addText("you win!", { y: 4, color: color`3` });
       scoreSun = 0
    scoreWater = 0
    scoreCo2 = 0
    }
  }
    
  
  
  

}, 1000-level*100);

